package com.kexuexiong.demo.bear.controller.mq;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.kexuexiong.demo.bear.common.config.RocketMQEnhanceTemplate;
import com.kexuexiong.demo.bear.common.dto.Result;
import com.kexuexiong.demo.bear.controller.BaseController;
import com.kexuexiong.demo.bear.mq.MemberMessage;
import com.kexuexiong.demo.bear.mq.RocketMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/api/mq/")
@Api(tags = {"消息队列"})
@Slf4j
public class NormalProduceController extends BaseController {

    @Setter(onMethod_ = @Autowired)
    private RocketMQTemplate rocketMQTemplate;

    @Setter(onMethod_ = @Autowired)
    private RocketMQEnhanceTemplate rocketMQEnhanceTemplate;

    private static final String topic = "rocket_enhance";

    private static final String tag = "member";

    /**
     * rockmq生产者测试普通消息
     *
     * @param msg
     * @return
     */
    @RequestMapping(value = "/produce", method = {RequestMethod.GET})
    @ApiOperation(value = "produce", notes = "普通生产者测试")
    public Result<String> rocketMQProduce(@Parameter(name = "msg") @RequestParam("msg") String msg,@Parameter(name = "topic") @RequestParam("topic") String topic) {
        log.info("rocketMQProduce,msg=>{}", msg);
        Message<String> message = MessageBuilder
                .withPayload(msg).build();
        rocketMQTemplate.send(topic,message);
        return Result.success();
    }

    /**
     * rockmq生产者测试普通消息
     *
     * @param msg
     * @return
     */
    @RequestMapping(value = "/rocketMQV2Produce", method = {RequestMethod.GET})
    @ApiOperation(value = "rocketMQV2Produce", notes = "v2生产者测试")
    public Result<String> rocketMQV2Produce(@Parameter(name = "msg") @RequestParam("msg") String msg,@Parameter(name = "topic") @RequestParam("topic") String topic) {
        log.info("rocketMQV2Produce,msg=>{}", msg);
        RocketMessage rocketMessage = RocketMessage.builder().message(msg)
                .id(RandomUtil.randomInt())
                .date(DateUtil.format(new Date(),"YYYY-MM-dd HH:mm:ss"))
                .build();
        rocketMQTemplate.convertAndSend(topic,rocketMessage);
        return Result.success();
    }

    /**
     * rockmq生产者测试普通消息
     *
     * @param msg
     * @return
     */
    @RequestMapping(value = "/rocketMQV3Produce", method = {RequestMethod.GET})
    @ApiOperation(value = "rocketMQV3Produce", notes = "v3生产者测试")
    public Result<String> rocketMQV3Produce(@Parameter(name = "msg") @RequestParam("msg") String msg) {
        log.info("rocketMQV3Produce,msg=>{}", msg);
        MemberMessage memberMessage = new MemberMessage();
        memberMessage.setMsg(msg);
        memberMessage.setKey(RandomUtil.randomString(8));
        memberMessage.setSource("member");
        rocketMQEnhanceTemplate.send(topic,tag,memberMessage);
        return Result.success();
    }

    /**
     * rockmq生产者测试普通消息 延时消息
     *
     * @param msg
     * @return
     */
    @RequestMapping(value = "/rocketMQV4Produce", method = {RequestMethod.GET})
    @ApiOperation(value = "rocketMQV4Produce", notes = "v4生产者测试")
    public Result<String> rocketMQV4Produce(@Parameter(name = "msg") @RequestParam("msg") String msg) {
        log.info("rocketMQV3Produce,msg=>{}", msg);
        MemberMessage memberMessage = new MemberMessage();
        memberMessage.setMsg(msg);
        memberMessage.setKey(RandomUtil.randomString(8));
        memberMessage.setSource("member");
        rocketMQEnhanceTemplate.send(topic,tag,memberMessage,3);
        return Result.success();
    }


}
